package com.erp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.erp.po.Providers;
import com.erp.service.ProviderServiceImpl;
import com.erp.util.BootStrapPageHelper;
import com.erp.util.Page;
import com.google.gson.Gson;

/**
 * Servlet implementation class ProviderServlet
 */
@WebServlet("/admin/providers/ProviderServlet")
public class ProviderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ProviderServiceImpl pros;

	public ProviderServlet() {
		pros = new ProviderServiceImpl();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 处理中文乱码
		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");*/

		String op = request.getParameter("op");

		System.out.println("op: " + op + " ------- ProviderServlet");

		if ("showProvidersByPage".equals(op)) {
			// 分页查询
			showProvidersByPage(request, response);
		} else if("readyEditProviderById".equals(op)) {
			// 准备修改
			readyEditProviderById(request, response);
		} else if("doEditProviderById".equals(op)) {
			// 开始修改
			doEditProviderById(request, response);
		} else if("removeProviderById".equals(op)) {
			// 删除
			removeProviderById(request, response);
		} else if("addProvider".equals(op)) {
			// 添加
			addProvider(request, response);
		} else if("checkProviderNameByEdit".equals(op)) {
			// 修改校验ajax
			checkProviderNameByEdit(request, response);
		} else if("checkProviderNameByAdd".equals(op)) {
			// 增加校验ajax
			checkProviderNameByAdd(request, response);
		} else if("doSearchProvider".equals(op)) {
			// ajax 条件查询数据
			doSearchProvider(request, response);
		}

	}

	/**
	 * ajax 条件查询数据
	 */
	protected void doSearchProvider(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		int offset = Integer.parseInt(request.getParameter("offset"));
		
		int pageSize = Integer.parseInt(request.getParameter("limit"));
		int currentPage = (offset / pageSize) + 1;
		
		String providerName = request.getParameter("provName");
		
		Page<Providers> page = pros.findProvidersListByCondition(new Page<Providers>(currentPage, pageSize), new Providers(providerName));
		
		BootStrapPageHelper<Providers> pageHelper = new BootStrapPageHelper<Providers>();
		pageHelper.setTotal(page.getTotal());
		pageHelper.setRows(page.getList());
		
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(pageHelper));
	}

	/**
	 * 增加校验ajax
	 */
	protected void checkProviderNameByAdd(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String providName = request.getParameter("providName");
		String str = null;
		boolean f = pros.findProviderByName(providName);
		if(f) {
			str = "可用";
		} else {
			str = "重名";
		}
		
		response.getWriter().print(str);
		
		
	}

	/**
	 * 修改校验ajax
	 */
	protected void checkProviderNameByEdit(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int providerId = Integer.parseInt(request.getParameter("id"));
		String providName = request.getParameter("providName");
		String str = null;
		
		// id相同,不修改名称不构成重名
		Providers provider = pros.findProviderByPName(providName);
		
		// 根据名称查询记录
		boolean f = pros.findProviderByName(providName);
		if(f) {
			str = "可用";
		} else {
			if(provider.getProviderId() == providerId) {
				str = "可用";
			} else {
				str = "重名";
			}
		}
		
		response.getWriter().print(str);
	}

	/**
	 * 添加
	 */
	protected void addProvider(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		String providerName = request.getParameter("providName");
		String providerAdd = request.getParameter("providAdd");
		String providerTel = request.getParameter("providTel");
		String account = request.getParameter("providAccount");
		String email = request.getParameter("providEmail");
		
		int i = pros.addProviders(new Providers(providerName, providerAdd, providerTel, account, email));
		if(i == 302) {
			out.write("<script>alert('提示信息: 供应商添加成功.');location.href='searchProviders.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 供应商添加失败,有空字符串.');location.href='addProvider.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 供应商添加失败,该供应商名称已存在.');location.href='addProvider.jsp';</script>");
		} else if(i == 303) {
			out.write("<script>alert('提示信息: 供应商添加失败,未知错误,请联系管理员处理.');location.href='searchProviders.jsp';</script>");
		}
		
	}

	/**
	 * 删除
	 */
	protected void removeProviderById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		int providerId = Integer.parseInt(request.getParameter("id"));
		
		int i = pros.removeProvidersById(providerId);
		if(i == 302) {
			out.write("<script>alert('提示信息: 供应商删除成功.');location.href='searchProviders.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 供应商删除失败,该供应商下的商品不为空.');location.href='searchProviders.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 供应商删除失败,没有该供应商.');location.href='searchProviders.jsp';</script>");
		}
		
		
	}

	/**
	 * 开始修改
	 */
	protected void doEditProviderById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		
		int providerId = Integer.parseInt(request.getParameter("providId"));
		String providerName = request.getParameter("providName");
		String providerAdd = request.getParameter("providAdd");
		String providerTel = request.getParameter("providTel");
		String account = request.getParameter("providAccount");
		String email = request.getParameter("providEmail");
		
		int i = pros.modifyProvidersById(new Providers(providerId, providerName, providerAdd, providerTel, account, email));
		if(i == 302) {
			out.write("<script>alert('提示信息: 供应商修改成功.');location.href='searchProviders.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 供应商修改失败,有空字符串.');location.href='editProviderById.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 供应商修改失败,该供应商名称已存在.');location.href='editProviderById.jsp';</script>");
		}
	}

	/**
	 * 准备修改
	 */
	protected void readyEditProviderById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		Providers provider = pros.findProviderById(id);
		request.setAttribute("provid", provider);
		request.getRequestDispatcher("editProviderById.jsp").forward(request, response);
	}

	/**
	 *  分页显示供应商
	 */
	protected void showProvidersByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		
		Page<Providers> page = pros.findProvidersListByPage(new Page<Providers>(currentPage, 5));
		request.setAttribute("page", page);
		
		request.getRequestDispatcher("showAllProviders.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
